#ifndef _COMMAND_H_
#define _COMMAND_H_

#include <string>
#include <functional>
#include <mutex>
#include <condition_variable>
#include "hiredis/adapters/libev.h"
#include "hiredis/async.h"

class Command;
class Redix;

class Command {	
    public:
	typedef std::function<void(Command&)> Callback;
	Command(Redix* rdx, long id, const std::string& cmd,
		const Callback& callback);
	~Command();

	bool ok() const;

	redisReply* reply() { return m_reply; }

	void free();

	std::string cmd() const { return m_cmd; }

	Redix* const m_rdx;
	const long m_id; 
	const std::string m_cmd;

    private:
	Command(const Command&) = delete;
	Command& operator=(const Command&) = delete;

	void processReply(redisReply* r);

	void invoke() {
	    if (m_callback) {
		m_callback(*this);
	    }
	}

	void freeReply();

	redisReply* m_reply = nullptr;
	const Callback m_callback;

	friend class Redix;		
};

#endif
